package design.graduation.novelsystem.controller;

import design.graduation.novelsystem.constant.Constant;
import design.graduation.novelsystem.model.domian.CentOSEntity;
import design.graduation.novelsystem.model.domian.SSHRegisterEntity;
import design.graduation.novelsystem.model.dto.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: 洪少尉
 * @Date: 2020/4/18 20:25
 * @Description: 爬虫管理
 */
@RestController
@RequestMapping("crawler")
public class CrawlerController {
	@Autowired
	private CentOSEntity centOSEntity;
	@RequestMapping("start")
	public ResultObj start() throws Exception {
		// 主机
		SSHRegisterEntity sshRegisterEntity = new SSHRegisterEntity(centOSEntity);
		// 向主机发送命令
		sshRegisterEntity.send("nohup java -jar novel-crawler.jar&");
		return ResultObj.START_CRAWLER;
	}
	@RequestMapping("show")
	public ResultObj show() throws Exception {
		// 主机
		SSHRegisterEntity sshRegisterEntity = new SSHRegisterEntity(centOSEntity);
		// 向主机发送命令
		String shell = sshRegisterEntity.exec("netstat -lnp|grep 9090");
		if(StringUtils.isNotBlank(shell)){
			return new ResultObj(Constant.SUCCESS_CODE,"启动成功",shell);
		}
		return ResultObj.START_CRAWLER_EEOR;
	}
	@RequestMapping("kill")
	public ResultObj kill(String pid) throws Exception {
		// 主机1
		SSHRegisterEntity sshRegisterEntity = new SSHRegisterEntity(centOSEntity);
		// 向主机1发送命令
		sshRegisterEntity.exec("kill -9 "+pid);
		return ResultObj.KILL_CRAWLER;
	}
}
